package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author bairimengchang
 * @version 1.0
 * @project sky-take-out
 * @description C端购物车业务接口实现类
 * @date 2024/08/22 20:36:16
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO C端添加购物车数据传输数据模型
     * @return 添加购物车操作是否成功，返回 true 表示成功，false 表示失败
     */
    @Override
    public boolean addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //先去判断此商品是否已加入购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        log.info("传入到service的商品信息:{}", shoppingCart);
        //获取当前登录用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查询该用户下符合条件的购物车信息
        ShoppingCart shoppingCartByMySql = shoppingCartMapper.selectOneByShoppingCart(shoppingCart);

        //如果是重复加入的商品则执行更新语句
        if (shoppingCartByMySql != null){
            //数量在原有基础上加1
            log.info("原本菜品信息:{}", shoppingCartByMySql);
            shoppingCart.setNumber(shoppingCartByMySql.getNumber() + 1);
            //设置一下要修改数量的购物车表id
            shoppingCart.setId(shoppingCartByMySql.getId());
            log.info("更改后数据:{}", shoppingCart);
            //更新数据库
            int update = shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);
            log.info("新增相同的菜品或套餐，直接更改数量！");
            return update == 1;
        }
        //否则执行插入语句
        //判断是套餐的情况
        if (shoppingCart.getSetmealId() != null){
            Long setmealId = shoppingCart.getSetmealId();
            Setmeal setmeal = setmealMapper.selectByPrimaryKey(setmealId);
            //冗余字段
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setNumber(1);
            //这里的金额不会随着数量变化，相当于显示单价，总额由前端计算
            shoppingCart.setAmount(setmeal.getPrice());
        } else {
            //是菜品
            Long dishId = shoppingCart.getDishId();
            Dish dish = dishMapper.selectByPrimaryKey(dishId);
            //冗余字段
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setName(dish.getName());
            shoppingCart.setNumber(1);
            shoppingCart.setAmount(dish.getPrice());
        }
        shoppingCart.setCreateTime(LocalDateTime.now());
        int insert = shoppingCartMapper.insertSelective(shoppingCart);
        log.info("新增不同的菜品或套餐，执行插入操作！");
        return insert == 1;
    }

    /**
     * 查看购物车
     * @return 购物车列表
     */
    @Override
    public List<ShoppingCart> getShoppingCartList() {
        //获取当前登录用户的id
        Long userId = BaseContext.getCurrentId();

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectListByUserId(userId);
        log.info("购物车列表:{}", shoppingCartList);
        return shoppingCartList;
    }

    /**
     * 清空购物车
     * @return 清空购物车操作是否成功，返回 true 表示成功，false 表示失败
     */
    @Override
    public boolean cleanShoppingCart() {
        //获取当前登录用户的id
        Long userId = BaseContext.getCurrentId();
        //确保删除数量是用户当前购物车的数量
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectListByUserId(userId);
        //本质上是删除操作
        int delete = shoppingCartMapper.deleteByUserId(userId);
        return delete == shoppingCartList.size();
    }

    /**
     * 删除购物车
     * @param shoppingCartDTO 购物车数据传输数据模型
     * @return 删除购物车操作是否成功，返回 true 表示成功，false 表示失败
     */
    @Override
    public boolean subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前登录用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //判断是否只有一件该商品，如果为一件，直接删除，否则数量减一
        ShoppingCart shoppingCartByMySql = shoppingCartMapper.selectOneByShoppingCart(shoppingCart);
        if (shoppingCartByMySql.getNumber() == 1) {
            log.info("购物车中只有一件该商品，直接删除！");
            int delete = shoppingCartMapper.deleteByPrimaryKey(shoppingCartByMySql.getId());
            return delete == 1;
        } else {
            log.info("购物车中有大于一件该商品，数量减一！");
            //数量减一
            shoppingCart.setNumber(shoppingCartByMySql.getNumber() - 1);
            //设置要修改购物车表id
            shoppingCart.setId(shoppingCartByMySql.getId());
            int update = shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);
            return update == 1;
        }
    }
}
